package o;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.view.KeyEvent;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.application.pkgmanage.model.update.ApkUpgradeInfo;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import o.cku.c;
import o.ewm.n;

public class eju extends AsyncTask<Void, Void, List<ApkUpgradeInfo>> {
    private static int ˏ;
    private List<ApkUpgradeInfo> ˊ = new ArrayList();
    private Activity ˋ;
    private ejv ˎ;
    private ekd ॱ;

    final class a extends SafeBroadcastReceiver {
        final /* synthetic */ eju ˎ;
        private final WeakReference<Activity> ॱ;

        public a(eju o_eju, Activity activity) {
            this.ˎ = o_eju;
            this.ॱ = new WeakReference(activity);
        }

        public void onReceiveMsg(Context context, Intent intent) {
            if ("android.net.wifi.STATE_CHANGE".equals(intent.getAction())) {
                NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra("networkInfo");
                if (networkInfo != null && State.CONNECTED == networkInfo.getState()) {
                    int ॱ = dvt.ॱ(context);
                    if (ॱ == 0 || eju.ˏ == ॱ) {
                        Logger.ˎ("CheckHmsOrPayHaveUpgradeTask", "Keep current download dialog");
                        return;
                    }
                    Logger.ˎ("CheckHmsOrPayHaveUpgradeTask", "network has changed,close the download dialog, lastNetType=" + eju.ˏ + " newNetType=" + ॱ);
                    Activity activity = (Activity) this.ॱ.get();
                    if (activity != null) {
                        activity.finish();
                    }
                    if (this.ˎ.ॱ != null) {
                        this.ˎ.ॱ.ˎ(false);
                    }
                }
            }
        }
    }

    final class b implements o.ckh.e {
        private BroadcastReceiver ˎ;
        final /* synthetic */ eju ˏ;

        public b(eju o_eju) {
            this.ˏ = o_eju;
        }

        public void ॱ(Activity activity) {
            this.ˎ = new a(this.ˏ, activity);
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.net.wifi.STATE_CHANGE");
            activity.registerReceiver(this.ˎ, intentFilter);
        }

        public void ˋ(Activity activity) {
            if (this.ˎ != null) {
                activity.unregisterReceiver(this.ˎ);
            }
        }
    }

    class d implements cks {
        private final List<ApkUpgradeInfo> ˋ;
        final /* synthetic */ eju ˎ;
        private final Activity ˏ;

        final class c implements o.ckh.a {
            final /* synthetic */ d ˋ;
            private ApkUpgradeInfo ˎ;

            public c(d dVar, ApkUpgradeInfo apkUpgradeInfo) {
                this.ˋ = dVar;
                this.ˎ = apkUpgradeInfo;
            }

            public void ˏ(AlertDialog alertDialog, com.huawei.appgallery.foundation.ui.framework.widget.dialog.dialogactivity.DialogActivity.a aVar, int i) {
                if (aVar.ˏ() == null) {
                    Logger.e("CheckHmsOrPayHaveUpgradeTask", "view is null");
                } else if (-1 == i) {
                    dvr.ॱ(aVar.ˏ(), true);
                    ejr.ˏ().ˊ(this.ˎ, ejw.class, true, dgm.ˎ(this.ˋ.ˏ), false, 4);
                    if (this.ˋ.ˎ.ॱ != null) {
                        this.ˋ.ˎ.ॱ.ap_();
                    }
                } else if (-2 == i) {
                    dvr.ॱ(aVar.ˏ(), false);
                    this.ˋ.ॱ(this.ˎ);
                    if (this.ˋ.ˎ.ॱ != null) {
                        this.ˋ.ˎ.ॱ.ˎ(false);
                    }
                }
            }
        }

        final class d implements OnDismissListener {
            final /* synthetic */ d ˎ;
            private dwl ॱ;

            public d(d dVar, dwl o_dwl) {
                this.ˎ = dVar;
                this.ॱ = o_dwl;
            }

            public void onDismiss(DialogInterface dialogInterface) {
                dialogInterface.dismiss();
                if (this.ॱ != null && this.ॱ.ॱ() != null) {
                    this.ॱ.ॱ().ॱ();
                }
            }
        }

        public d(eju o_eju, Activity activity, List<ApkUpgradeInfo> list) {
            this.ˎ = o_eju;
            this.ˏ = activity;
            this.ˋ = list;
        }

        public void enableNetworking() {
            ˊ();
        }

        private void ˊ() {
            if (doo.ˊ(this.ˏ)) {
                ॱ();
                return;
            }
            evk.ˏ(this.ˏ, n.ʹˊ, 0).ˋ();
            if (this.ˎ.ॱ != null) {
                this.ˎ.ॱ.ˎ();
            }
        }

        private void ॱ() {
            int ˎ = dgm.ˎ(this.ˏ);
            ˊ((ApkUpgradeInfo) this.ˋ.get(0), ˎ);
            if (this.ˋ.size() == 2) {
                ˊ((ApkUpgradeInfo) this.ˋ.get(1), ˎ);
            }
        }

        private void ˊ(ApkUpgradeInfo apkUpgradeInfo, int i) {
            if (apkUpgradeInfo == null || TextUtils.isEmpty(apkUpgradeInfo.ᐝ())) {
                Logger.e("CheckHmsOrPayHaveUpgradeTask", "error info.");
                return;
            }
            Logger.ˎ("CheckHmsOrPayHaveUpgradeTask", "start Task:" + apkUpgradeInfo.ᐝ() + ",ver:" + apkUpgradeInfo.ʿ());
            int ˋ = ((cdg) cdb.ॱ(cdg.class)).ˋ(apkUpgradeInfo.ᐝ());
            if (ˋ == 10 || ˋ == 11) {
                Logger.ˎ("CheckHmsOrPayHaveUpgradeTask", "installing...");
                return;
            }
            boolean z;
            if (ˋ == 1 || ˋ == 2) {
                boolean ˊ = ((ceq) cdb.ॱ(ceq.class)).ˊ(this.ˏ, apkUpgradeInfo.ᐝ(), apkUpgradeInfo.ʿ());
                if (!ˊ) {
                    ((cdf) cdb.ॱ(cdf.class)).ॱ(apkUpgradeInfo.ᐝ());
                }
                z = ˊ;
            } else {
                z = false;
            }
            if (z) {
                SessionDownloadTask sessionDownloadTask = new SessionDownloadTask();
                sessionDownloadTask.ʼ(apkUpgradeInfo.ᐝ());
                sessionDownloadTask.ˊ(apkUpgradeInfo.ˈ());
                sessionDownloadTask.ॱॱ(apkUpgradeInfo.ˊ());
                sessionDownloadTask.ॱ(apkUpgradeInfo.ˏˎ());
                ejr.ˏ().ˋ(sessionDownloadTask, new ejy());
            } else if (dvr.ˏ(this.ˏ, true)) {
                ejr.ˏ().ˊ(apkUpgradeInfo, ejw.class, true, i, false, 4);
                if (this.ˎ.ॱ != null) {
                    this.ˎ.ॱ.ap_();
                }
            } else {
                ˋ(apkUpgradeInfo);
            }
        }

        public void upgrade() {
            if (this.ˎ.ॱ != null) {
                this.ˎ.ॱ.ˎ(false);
            }
        }

        public void ˋ() {
        }

        private void ॱ(ApkUpgradeInfo apkUpgradeInfo) {
            dwb.ˋ(apkUpgradeInfo.ˊ());
            ejr.ˏ().ˎ(apkUpgradeInfo, new o.dkb.d(this) {
                final /* synthetic */ d ॱ;

                {
                    this.ॱ = r1;
                }

                public void ˋ(SessionDownloadTask sessionDownloadTask) {
                    if (sessionDownloadTask != null) {
                        dvt.ˏ().ॱ(sessionDownloadTask);
                    }
                }
            });
        }

        private void ˋ(ApkUpgradeInfo apkUpgradeInfo) {
            long ʼ;
            if (apkUpgradeInfo.ʼ() > 0) {
                ʼ = apkUpgradeInfo.ʼ();
            } else {
                ʼ = apkUpgradeInfo.ʻॱ();
            }
            Context ˋ = ContentManager.getInstant().getContext();
            dwl o_dwl = new dwl();
            o.ckh.a cVar = new c(this, apkUpgradeInfo);
            OnDismissListener dVar = new d(this, o_dwl);
            o.ckh.e bVar = new b(this.ˎ);
            OnKeyListener eVar = new e();
            if (dvr.ˏ(ˋ)) {
                eju.ˏ = 1;
                dvr.ˏ(ˋ, ʼ, cVar, dVar, bVar, eVar);
            } else if (dvr.ॱ(ˋ)) {
                ॱ(apkUpgradeInfo);
                if (this.ˎ.ॱ != null) {
                    this.ˎ.ॱ.ˎ(false);
                }
            } else if (dvr.ˋ(ˋ)) {
                eju.ˏ = 2;
                dvr.ˊ(ˋ, ʼ, cVar, dVar, bVar, eVar);
            }
        }
    }

    class e implements OnKeyListener {
        final /* synthetic */ eju ˏ;

        private e(eju o_eju) {
            this.ˏ = o_eju;
        }

        public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
            if (i != 4) {
                return false;
            }
            dialogInterface.dismiss();
            if (this.ˏ.ॱ != null) {
                this.ˏ.ॱ.ˎ(false);
            }
            return true;
        }
    }

    protected /* synthetic */ Object doInBackground(Object[] objArr) {
        return ˊ((Void[]) objArr);
    }

    protected /* synthetic */ void onPostExecute(Object obj) {
        ˏ((List) obj);
    }

    public eju(Activity activity, ejv o_ejv, ekd o_ekd) {
        this.ˋ = activity;
        this.ˎ = o_ejv;
        this.ॱ = o_ekd;
    }

    protected void onPreExecute() {
        super.onPreExecute();
    }

    protected List<ApkUpgradeInfo> ˊ(Void... voidArr) {
        ApkUpgradeInfo ˊ;
        Logger.ˎ("CheckHmsOrPayHaveUpgradeTask", "CheckHmsAndPayUpgradeTask doInBackground");
        if (this.ˎ.ˊ() != null) {
            ˊ = dss.ॱ().ˊ(this.ˋ, this.ˎ.ˊ(), this.ˎ.ˏ(), 19, 1);
            if (ˊ != null) {
                this.ˊ.add(ˊ);
            }
        }
        if (this.ˎ.ॱ() != null) {
            ˊ = dss.ॱ().ˊ(this.ˋ, this.ˎ.ॱ(), this.ˎ.ˎ(), 19, 1);
            if (ˊ != null) {
                this.ˊ.add(ˊ);
            }
        }
        return this.ˊ;
    }

    protected void ˏ(List<ApkUpgradeInfo> list) {
        if (!list.isEmpty()) {
            ˊ(this.ˋ, list, this.ˎ);
        } else if (this.ॱ != null) {
            this.ॱ.ˏ();
        }
    }

    private void ˊ(Activity activity, List<ApkUpgradeInfo> list, ejv o_ejv) {
        String ˋ = o_ejv.ˋ();
        CharSequence ʼ = o_ejv.ʼ();
        CharSequence ʻ = o_ejv.ʻ();
        cku ˏ = cku.ˏ(activity, cku.class, null, ˏ(activity, list, ˋ));
        ˏ.ॱ(-1, ʼ);
        ˏ.ॱ(-2, ʻ);
        if (azc.ˎ().ॱ() >= 11 && azc.ˎ().ॱ() < 17) {
            c cVar = new c();
            cVar.ˋ(o.ewm.e.ﾟ);
            cVar.ˊ(activity.getResources().getColor(ewm.c.ॱˎ));
            ˏ.ॱ(-1, cVar);
        }
        ˏ.ॱ(activity, "updateOTADialog");
        ˏ.ॱ(new d(this, activity, list));
        ˏ.ˏ(new e());
    }

    private String ˏ(Activity activity, List<ApkUpgradeInfo> list, String str) {
        if (list.size() == 1) {
            return str.replace("%P", ((ApkUpgradeInfo) list.get(0)).ˊ() + "(" + epw.ˊ(((ApkUpgradeInfo) list.get(0)).ʻॱ()) + ")");
        }
        if (list.size() != 2) {
            return str;
        }
        return str.replace("%P", activity.getString(n.ˎߺ, new Object[]{((ApkUpgradeInfo) list.get(0)).ˊ(), epw.ˊ(((ApkUpgradeInfo) list.get(0)).ʻॱ()), ((ApkUpgradeInfo) list.get(1)).ˊ(), epw.ˊ(((ApkUpgradeInfo) list.get(1)).ʻॱ())}));
    }
}
